package com.demo.common.redis.stream;

import cn.hutool.core.date.DatePattern;
import cn.hutool.extra.mail.MailUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 消费错误处理
 * </p>
 *
 * @author molong
 * @date 2022/6/22
 */
public class ErrorProcess {

    public static void execution(String email, String streamKey, String exception, String msgId){
        //发送邮件
        MailUtil.send(email, "系统消息处理异常", getHtmlContent(streamKey, exception, msgId), true);
    }

    private static String getHtmlContent(String streamKey, String exception, String msgId){
        exception = exception.replaceAll("\\r\\n", "</br>");
        exception = exception.replaceAll("\\t", "&emsp;");
        return  "<table>" +
                "<tr>" +
                "<td style='width:100px'>key: </td>" +
                "<td>"+ streamKey +"</td>" +
                "</tr>" +
                "<tr>" +
                "<td style='width:100px'>消息id: </td>" +
                "<td>"+ msgId +"</td>" +
                "</tr>" +
                "<tr>" +
                "<td style='width:100px'>时间: </td>" +
                "<td>"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)) +"</td>" +
                "</tr>" +
                "<tr>" +
                "<td>异常信息: </td>" +
                "<td>"+ exception +"</td>" +
                "</tr>" +
                "</table>";
    }
}
